<?php

namespace addons\Contact\api\modules\v1\controllers;
use Yii;
use yii\data\ActiveDataProvider;
use api\controllers\OnAuthController;
use common\enums\StatusEnum;
use common\models\merchant\Member;
use common\models\rbac\AuthRole;

/**
 * 默认控制器
 *
 * Class DefaultController
 * @package addons\Contact\api\modules\v1\controllers
 */
class DefaultController extends OnAuthController
{
    public $modelClass = Member::class;

    /**
    * 不用进行登录验证的方法
    *
    * 例如： ['index', 'update', 'create', 'view', 'delete']
    * 默认全部需要验证
    *
    * @var array
    */
    protected $authOptional = ['index'];

    /**
    * 首页
    *
    * @return string
    */
    public function actionIndex()
    {
        return new ActiveDataProvider([
            'query' => $this->modelClass::find()
                ->select('*')
                ->where(['status' => StatusEnum::ENABLED])
                ->andFilterWhere(['merchant_id' => Yii::$app->user->identity->merchant_id])
                ->asArray()
                ->with('assignment','department','role'),
            'pagination' => [
                'pageSize' => $this->pageSize,
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);
    }
    public function actionRole($id)
    {
        return AuthRole::find()->select('title')->where(['id'=>$id])->scalar();
    }
}